import {request} from "./request";
import {baseUrl, get} from "@/utils/requests/requests";
import axios from "axios";

// 获取订单列表
export function getOrderListRequest(params) {
    return request({
        url: '/orders',
        params
    })
}

// 获取物流进度
export function getOrderDetailRequest(id) {
    return request({
        url: `/orders/${id}`
    })
}

// 修改订单数据
export function editOrderRequest(id, data) {
    return request({
        url: `/orders/${id}`,
        method: 'put',
        data
    })
}

// 下载订单数据
export function downloadOrderDataRequest(type, startTime, endTime) {
    // get({
    //     url:`/admin/order/exportOrderListByDate/${type}/${startTime}/${endTime}`,
    //     getHeader: true
    // }).then(response => {
    //     axiosDownload(response)
    // })
    window.open(`${baseUrl}/admin/order/exportOrderListByDate/${type}/${startTime}/${endTime}`,"__blank")
}


// axios流下载
const axiosDownload = (response) => {
    const blob = new Blob([response.data], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'});
    const downloadElement = document.createElement('a');
    const href = window.URL.createObjectURL(blob);
    let contentDisposition = response.headers['content-disposition'];  //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
    let part = new RegExp("filename=([^;]+\\.[^\\.;]+);*");
    let result = part.exec(contentDisposition);
    let filename = decodeURI(result[1]);
    downloadElement.style.display = 'none';
    downloadElement.href = href;
    downloadElement.download = String(Math.random()).slice(2) + '.xlsx'; //下载后文件名
    document.body.appendChild(downloadElement);
    downloadElement.click(); //点击下载
    document.body.removeChild(downloadElement); //下载完成移除元素
    return true;
}
